/**
* 删除左右两端的空格
*/
String.prototype.trim=function()
{
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
};
/**
* 删除左边的空格
*/
String.prototype.ltrim=function()
{
     return this.replace(/(^\s*)/g,"");
};
/**
* 删除右边的空格
*/
String.prototype.rtrim=function()
{
     return this.replace(/(\s*$)/g,"");
};

/**
 * 系统用到的公共javascript方法
 */
var syscore = {};
/**
 * javascript检索内容(不区分大小写)
 */



$.ajaxSetup({
	  cache:false,
	  type: "POST"
	  //success:function(data,textStatus){
		 
	  //}
	});
/**
 * 设置DataGrid方法
 * @param targetObj  table元素
 * @param pager		分页控件
 * @param url		url地址
 * @param colNames	列头信息
 * @param colModel	列信息
 * @param multiselect	是否允许多选
 * @param height	Grid高度
 * @param id	ID键
 */
syscore.setGridData = function(targetObj,pager,url,colNames,colModel,multiselect,height,id,loadComplete,onSelectRow){
	$(targetObj).jqGrid({
		url:url,
		mtype:'post',
		datatype:'json',
		height:height?height:250,
		multiselect: multiselect ? true : false,
		multiboxonly:true,
		autowidth:true,
		altRows:false,
		autoencode:true,
		viewrecords:true,
		forceFit:true,
		gridview:true,
		loadui:'block',
		rowNum:0,
		sortorder:'asc',
		rownumbers:true,
		colNames:colNames,
		colModel:colModel,
		jsonReader:{
			id:id ? id : 'id',
			root:'result.data',
			page:'result.page.curPageNo',
			total:'result.page.totalPage',
			repeatitems:false
		},
		prmNames:{
			page:'page.curPageNo',
			rows:'page.pageSize',
			sort:'orderField',
			order:'orderType'			
			//sort:'orderBy.sidx',
			//order:'orderBy.sord'
		},
		page:1,
		pagerpos:'left',
		pager:pager,
		loadComplete:loadComplete,
		onSelectRow:onSelectRow
	});
};

/**
 * ajax Form提交
 * @param targetForm 要提交的form
 * @param callFun	提交成功后的回调方法
 */
syscore.ajaxSubmit = function(targetForm,callFun){
	$(targetForm).unbind("submit");
	$(targetForm).submit(function(){
		$(this).ajaxSubmit({
			contentType: "application/x-www-form-urlencoded; charset=utf-8", 
			dataType:'json',
			beforeSubmit:function(formData,jqForm,options){
				//此处用于处理提交之前的信息
				return true;
			},
			success:function(rsData,statusText,xhr,$form){
				
				callFun(rsData,statusText,xhr,$form);
			}
		});
		return false;
	});
	$(targetForm).submit();
};

/**
 * 用于新增、修改弹出的窗口(直接通过url返回页面信息)
 * @param type	新增还是修改
 * @param title 标题
 * @param loadUrl	页面url
 * @param loadParam	获取页面时的参数
 * @param width	窗口宽度
 * @param height 窗口高度 
 * 
 * @returns 返回dialog对象
 */ 
syscore.editDialog = function(title,loadUrl,loadParam,width,height,saveUrl,callFun,onLoad){
	var dlg = $("<div/>").dialog({
		modal:true,
		header:true,
		height:height,
		width:width,
		title:title,
		closable:true,
		minimizable:false,
		collapsible:false,
		iconCls:'icon',
		loadUrl:syscore.mergeUrl(loadUrl,loadParam),
		buttons:[
		    {text:'保  存',type:'button',iconCls:'icon-save',handler:function(){
		    	dlg.find("form").attr("action",saveUrl);
		    	syscore.ajaxSubmit(dlg.find("form"),callFun);
		    }},
		    {text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
		    	dlg.dialog("destroy");
		    }}
		],
		onLoad:onLoad
	});
	dlg.dialog("loadPage");
	return dlg;
};

/**
 * 合并URL
 */
syscore.mergeUrl = function(url,params){
	
	if($.trim(params) == '') return url;
	if(url.indexOf('?') == -1){
		url += "?" + params;		
	}else{
		url += "&" + params;
	}
	return url;
};

/**
 * 将Form对象转换成JSON格式数据
 */
syscore.formToJson = function(targetForm){
	var params = $(targetForm).serializeArray();
	var paramJson = "";
	for(var i = 0;i < params.length;i++){
		if($.trim(params[i].name) == "") continue;
		if(i < params.length - 1){
			paramJson += "'" + params[i].name + "':'" + params[i].value + "',";
		}else{
			paramJson += "'" + params[i].name + "':'" + params[i].value + "'";
		}
	}
	paramJson = "{"+paramJson+"}";
	paramJson = eval('('+paramJson+')');
	return paramJson;
};

/**
 * json对象转字符串
 */
syscore.jsonToStr = function(O) {
	// return JSON.stringify(jsonobj);

	var S = [];
	var J = "";
	if (Object.prototype.toString.apply(O) === '[object Array]') {
		for ( var i = 0; i < O.length; i++)
			S.push(O2String(O[i]));
		J = '[' + S.join(',') + ']';
	} else if (Object.prototype.toString.apply(O) === '[object Date]') {
		J = "new Date(" + O.getTime() + ")";
	} else if (Object.prototype.toString.apply(O) === '[object RegExp]'
			|| Object.prototype.toString.apply(O) === '[object Function]') {
		J = O.toString();
	} else if (Object.prototype.toString.apply(O) === '[object Object]') {
		for ( var i in O) {
			O[i] = typeof (O[i]) == 'string' ? '"' + O[i] + '"'
					: (typeof (O[i]) === 'object' ? O2String(O[i]) : O[i]);
			S.push(i + ':' + O[i]);
		}
		J = '{' + S.join(',') + '}';
	}

	return J;
}; 

/**
 * 根据状态值返回状态显示文本
 * @param cellValue
 * @returns {String}
 */
syscore.formaterStatus = function(cellValue){
	if(cellValue == "normal"){
		return "正常";
	}else if(cellValue == "deleted"){
		return "已删除";
	}else if(cellValue == "disabled"){
		return "不可用";
	}else{
		return "未知";
	}	
};

/**
 * 是否审核
 */
syscore.formaterCheckStatus = function(cellValue){
	if(cellValue == "unAudited"){
		return "待审核";
	}else if(cellValue == "audited"){
		return "已审核";
	}else if(cellValue == "notAudited"){
		return "未通过";
	}else if(cellValue=="untreated"){
		return "未处理";
	}else{
		return  "未知状态";
	}
}